package work.sajor.wechatpush.util;

import org.apache.commons.lang3.StringUtils;

import java.io.*;


public class FileDealUtils {

    /**
     * 创建文件
     * @param file
     * @return
     */
    public static boolean createFile(File file)throws Exception{
        boolean flag=false;
        try{
            if(file.exists()){
                file.delete();
            }
                file.createNewFile();
                flag=true;
        }catch(Exception e){
            e.printStackTrace();
        }
        return true;
    }

    /**
     * 读TXT文件内容
     * @param filePath
     * @return
     */
    public static String readTxtFile(String filePath)throws Exception{
        File file=new File(filePath);
        String result="";
        FileReader fileReader=null;
        BufferedReader bufferedReader=null;
        try{
            fileReader=new FileReader(file);
            bufferedReader=new BufferedReader(fileReader);
            try{
                String read=null;
                while((read=bufferedReader.readLine())!=null){
                    if(read.contains("@JsonProperty")){
                        String begin= read.substring(0, read.indexOf("\"")+1);
                        String end= read.substring(read.lastIndexOf("\""));
                        String lower = read.substring(read.indexOf("\"")+1 , read.lastIndexOf("\""));
                        String uper = CharacterConversionUtil.underscoreName(lower);
                        read=begin+uper+end;
                    }
                    if(read.contains("@ApiModelProperty")){
                        String begin= read.substring(0, read.indexOf("\"")+1);
                        String surplus= read.substring(read.indexOf("\"") + 1);
                        String end= surplus.substring(surplus.indexOf("\""));
                        String lower = surplus.substring(0, surplus.indexOf("\""));
                        String uper = CharacterConversionUtil.underscoreName(lower);
                        read=begin+uper+end;
                    }


                    result=result+read+"\r\n";
                }
            }catch(Exception e){
                e.printStackTrace();
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            if(bufferedReader!=null){
                bufferedReader.close();
            }
            if(fileReader!=null){
                fileReader.close();
            }
        }
        System.out.println("读取出来的文件内容是："+"\r\n"+result);
        return result;
    }

    /**
     * 写文件
     * @param filePath
     * @return
     */
    public static boolean writeTxtFile(String content,String filePath  )throws Exception{
        File file=new File(filePath);
        createFile(file);
        RandomAccessFile mm=null;
        boolean flag=false;
        FileOutputStream o=null;
        try {
            o = new FileOutputStream(file);
            o.write(content.getBytes("UTF-8"));
            o.close();
//   mm=new RandomAccessFile(file,"rw");
//   mm.writeBytes(content);
            flag=true;
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }finally{
            if(mm!=null){
                mm.close();
            }
        }
        return flag;
    }



    public static void contentToTxt(String filePath, String content) {
        String str = new String(); //原有txt内容
        String s1 = new String();//内容更新
        try {
            File f = new File(filePath);
            if (f.exists()) {
                System.out.print("文件存在");
            } else {
                System.out.print("文件不存在");
                f.createNewFile();// 不存在则创建
            }
            BufferedReader input = new BufferedReader(new FileReader(f));

            while ((str = input.readLine()) != null) {
                s1 += str + "\n";
            }
            System.out.println(s1);
            input.close();
            s1 += content;

            BufferedWriter output = new BufferedWriter(new FileWriter(f));
            output.write(s1);
            output.close();
        } catch (Exception e) {
            e.printStackTrace();

        }
    }
    public static Integer getFileCount(File file){
        int linenumber = 0;
        try {
            FileReader fr = new FileReader(file);
            LineNumberReader lnr = new LineNumberReader(fr);
            while (StringUtils.isNotBlank(lnr.readLine())){
                linenumber++;
            }
        }catch (Exception e){
            e.printStackTrace();
        }

        return linenumber;
    }


//    public static void main(String[] args) {
////        String filePath="E:\\WorkSpace\\2I\\0108-tz\\trades_batchbusid\\src\\main\\java\\com\\unicom\\microserv\\trades\\domain\\";
////        String filePathNew="C:\\Users\\Administrator\\Desktop\\new\\";
//        String filePath="E:\\WorkSpace\\2I\\0108-tz\\trades_batchbusid\\src\\main\\java\\com\\unicom\\microserv\\trades\\utils\\";
//        String filePathNew="C:\\Users\\Administrator\\Desktop\\newutil\\";
//        List<String> list=new ArrayList<>();
////        list.add("QueryRelationInfoRspBO.java");
////        list.add("CardNumInfoRspBO.java");
////        list.add("CardDataInfoBO.java");
////        list.add("QueryRelationInfoReqBO.java");
////        list.add("CheckBatchCardRspBO.java");
////        list.add("NotifyCardNumProductReqBO.java");
////        list.add("BatchCallSimpleReqBO.java");
////        list.add("CardNumForNotifyRspInfoBO.java");
////        list.add("CallBatchRspBO.java");
////        list.add("CallBatchDetailnfo.java");
////        list.add("CardNumberReqBO.java");
////        list.add("CardNumberRspBO.java");
////        list.add("FileUpload2OssReqBO.java");
////        list.add("FileUpload2OssRspBO.java");
////        list.add("ExportExcelBusiReqBO.java");
////        list.add("ExportExcelBusiRspBO.java");
////        list.add("GetCardCheckByConditionRspBO.java");
////        list.add("GetCardCheckByFileRspBO.java");
////        list.add("GetCardCheckCostCalcRspBO.java");
////        list.add("ModifyLastDateCheckReqBO.java");
////        list.add("ModifyLastDateCheckRspBO.java");
////        list.add("ModifyLastDateModifyReqBO.java");
////        list.add("ModifyLastDateModifyRspBO.java");
////        list.add("ModifyLastDateReqBO.java");
////        list.add("ModifyLastDateRspBO.java");
////        list.add("DemoTableReqBO.java");
////        list.add("DemoTableRspBO.java");
////        list.add("PreOpenAcctByConditionRspBO.java");
////        list.add("PreOpenAcctGenNodeFileRspBO.java");
////        list.add("BatchCallFileReqBO.java");
////        list.add("PreOpenProductReqBO.java");
////        list.add("PreOpenProductRspBO.java");
////        list.add("QueryAgencyContractRspBO.java");
////        list.add("ReturnCardsCombReqBO.java");
////        list.add("ReturnCardsCombRspBO.java");
////        list.add("TempFileParsReqBO.java");
////        list.add("TempFileParsRspBO.java");
////        list.add("DataPreviewReqBO.java");
////        list.add("DataPreviewRspBO.java");
//
//
//
////        String fileName="BatchBusiProductTypeBO.java";
//
//        list.add("BaseRspBO.java");
//        list.add("ReqPage.java");
//        list.add("ReqSatffPage.java");
//        list.add("ReqStaffInfoBO.java");
//        list.add("RspPageBO.java");
//        for(String fileName:list) {
//            try {
//                String fileline = readTxtFile(filePath + fileName);
//                writeTxtFile(fileline, filePathNew + fileName);
//            } catch (Exception e) {
//                e.printStackTrace();
//            }
//        }
//    }
}
